
const DEFAULT_CHUNK_SIZE = 1024*64
// const sleep = (ms) => new Promise(res => setTimeout(res, ms))

export default class UrlReader {
    opts = {};
    text = "";
    finished = false;
    reader = null;
    constructor(opts) {
        this.opts = opts
    }

    async read() {
        const chunkSize = this.opts.chunkSize || DEFAULT_CHUNK_SIZE
        if (this.finished) return null
        if (!this.reader) {
            const r = await fetch(this.opts.url, this.opts.reqOptions)
            if (!r.body) throw new Error("no body in response")
            this.reader = r.body.getReader()
        }
        const decoder = new TextDecoder()
        while (this.text.length < chunkSize) {
            const {value, done} = await this.reader.read()
            if (done || !value) {
                break
            } else {
                this.text += decoder.decode(value)
            }
        }
        if (this.text.length >= chunkSize) {
            const toReturn = this.text.slice(0, chunkSize)
            this.text = this.text.slice(chunkSize)
            return toReturn
        } else {
            const toReturn = this.text
            this.text = ""
            this.finished = true
            return toReturn
        }
    }
}
